package appnode;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.logging.Logger;
import javax.jdo.PersistenceManager;
import javax.servlet.http.*;
import javax.servlet.*;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import javax.jdo.Query;

import appnode.Node;
import appnode.Interface;
import appnode.SearchNode;
import appnode.PMF;

public class EditInterface extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException  {
	
	PersistenceManager pm = PMF.get().getPersistenceManager();
	
	String keystrif = req.getParameter("keystrif");
	Key key = KeyFactory.stringToKey(keystrif);
	Interface interfc = pm.getObjectById(Interface.class,key);

	
	String name = req.getParameter("name");
	String protocol = req.getParameter("protocol");
	String direction = req.getParameter("direction");
	String delete = req.getParameter("delete");
	
	if (delete.equals("")) {
		// same name
		int check = 0;
		if (!name.equals(interfc.getName())) {
			for (int i=0;i<interfc.getNode().getInterfaces().size();i++) {
		 		if (name.equals(interfc.getNode().getInterfaces().get(i).getName())) {
					check++; // same name
				}
			} // for	
		}
		if (check==0) {
			interfc.setName(name);
			interfc.setProtocol(protocol);
			interfc.setDirection(direction);
			resp.setContentType("text/html");
			resp.getWriter().print("<html>");
			//resp.getWriter().print("<link rel='stylesheet' href='map.css' type='text/css' media='screen' title='mapcss'></link>");
			resp.getWriter().println("<head><title>Edit interface</title></head>");
			resp.getWriter().print("<div style='font-family:Verdana;font-size: smaller;'>You change the interface succesfully</div>");
			resp.getWriter().print("<br>");
			resp.getWriter().print("<input type='button' value='Ok' onClick='self.close()'/>");
			resp.getWriter().print("</html>");
		} 
		else {
			resp.setContentType("text/html");
			resp.getWriter().print("<html>");
			resp.getWriter().print("<link rel='stylesheet' href='map.css' type='text/css' media='screen' title='mapcss'></link>");
			resp.getWriter().println("<head><title>Edit interface</title></head>");
			resp.getWriter().print("Same name.Try again!");
			resp.getWriter().print("<input type='button' value='Ok' onClick='self.close()'/>");
			resp.getWriter().print("</html>");
		} // if
	} // delete
	else {
		pm.deletePersistent(interfc);
		resp.setContentType("text/html");
		resp.getWriter().print("<html>");
		//resp.getWriter().print("<link rel='stylesheet' href='map.css' type='text/css' media='screen' title='mapcss'></link>");
		resp.getWriter().println("<head><title>Edit interface</title></head>");
		resp.getWriter().print("<div style='font-family:Verdana;font-size: smaller;'>You deleted your interface</div><p>");
		resp.getWriter().print("<input type='button' value='Ok' onClick='self.close()'/>");
		resp.getWriter().print("</html>");
	}
		
	pm.close();
	}

}